package cn.shafish.cachedemo.config;


import cn.shafish.cachedemo.multilevelcache.properties.CacheConfigProperties;
import cn.shafish.cachedemo.multilevelcache.support.RedisCaffeineCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;

@Configuration
public class CacheConfig {

    @Autowired
    private CacheConfigProperties cacheConfigProperties;

    @Resource(name = "stringKeyRedisTemplate")
    private RedisTemplate<Object, Object> stringKeyRedisTemplate;

    @Bean
    @Primary
    public RedisCaffeineCacheManager redisCaffeineCacheManager() {
        return new RedisCaffeineCacheManager(cacheConfigProperties, stringKeyRedisTemplate);
    }

    /**
     * 配置Caffeine的缓存管理器，用于管理我們的緩存cache
     *
     * @return 缓存管理器
     @Bean("caffeineCacheManager") public CacheManager cacheManager() {
     CaffeineCacheManager cacheManager = new CaffeineCacheManager();
     cacheManager.setCaffeine(Caffeine.newBuilder()
     // 设置最后一次写入或访问后经过固定时间过期
     .expireAfterAccess(60, TimeUnit.SECONDS)
     // 初始的缓存空间大小
     .initialCapacity(100)
     // 缓存的最大条数
     .maximumSize(1000));
     return cacheManager;
     }
     */
}
